package org.itzixi.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.itzixi.enums.YesOrNo;
import org.itzixi.mapper.FriendshipMapper;
import org.itzixi.pojo.Friendship;
import org.itzixi.pojo.dto.FriendshipDTO;
import org.itzixi.pojo.vo.ContactsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class FriendshipService {

    private final FriendshipMapper friendshipMapper;

    private final PlatformTransactionManager platformTransactionManager;

    private final TransactionDefinition transactionDefinition;
    public Friendship getFriendship(String myId, String friendId) {


        return friendshipMapper.selectFriendshipByMyIdAndFriendId(myId, friendId);
    }

    public List<ContactsVO> queryMyFriends(String myId,boolean needBlack) {
        FriendshipDTO friendshipDTO = FriendshipDTO.builder()
                .myId(myId).needBlack(needBlack)
                .build();
        return friendshipMapper.queryMyFriends(friendshipDTO);
    }

    /**
     * 修改好友备注名称
     * @param myId
     * @param friendId
     * @param friendRemark
     */
    public void updateFriendRemark(String myId, String friendId, String friendRemark) {
        friendshipMapper.updateFriendRemark(myId, friendId, friendRemark);
    }

    public void updateBlackList(String myId, String friendId, YesOrNo yesOrNo) {
        friendshipMapper.updateBlackList(myId, friendId, yesOrNo);
    }

    public void delete(String myId, String friendId) {
        QueryWrapper<Friendship> deleteWrapper1 = new QueryWrapper<>();
        deleteWrapper1.lambda().eq(Friendship::getMyId, myId)
                .eq(Friendship::getFriendId, friendId);
        QueryWrapper<Friendship> deleteWrapper2 = new QueryWrapper<>();
        deleteWrapper2.lambda().eq(Friendship::getMyId, friendId)
                .eq(Friendship::getFriendId, myId);
        TransactionStatus transactionStatus = null;
        try {
            transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
            friendshipMapper.delete(deleteWrapper1);
//            platformTransactionManager.commit(transactionStatus);

            friendshipMapper.delete(deleteWrapper2);

            platformTransactionManager.commit(transactionStatus);
        }catch (Exception ex){
            log.error("删除好友出错：org.itzixi.service.FriendshipService.delete(String myId, String friendId):{},{}", ex, ex.getMessage());
            if (transactionStatus != null)
                platformTransactionManager.rollback(transactionStatus);
        }

    }

    public boolean isBlackEachOther(String friendId1st, String friendId2nd) {

        QueryWrapper<Friendship> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda()
                .eq(Friendship::getMyId, friendId1st)
                .eq(Friendship::getFriendId, friendId2nd)
                .eq(Friendship::getIsBlack, YesOrNo.YES.type);
        Friendship friend1 = friendshipMapper.selectOne(queryWrapper1);
        QueryWrapper<Friendship> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda()
                .eq(Friendship::getMyId, friendId2nd)
                .eq(Friendship::getFriendId, friendId1st)
                .eq(Friendship::getIsBlack, YesOrNo.YES.type);
        Friendship friend2 = friendshipMapper.selectOne(queryWrapper2);
        return friend1 != null || friend2 != null;
    }
}
